import 'package:fijkplayer/fijkplayer.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoPage extends StatefulWidget {
  const VideoPage({super.key});

  @override
  State<StatefulWidget> createState() => _VideoPageState();
}

class _VideoPageState extends State<VideoPage> {
  // late VideoPlayerController _controller;
  // late Future<void> _initializeVideoPlayerFuture;

  late FijkPlayer _player;

  @override
  void initState() {
    super.initState();
    // _controller = VideoPlayerController.networkUrl(
    //     Uri.parse("http://vjs.zencdn.net/v/oceans.mp4"));
    // _initializeVideoPlayerFuture = _controller.initialize();
    // _controller.setLooping(true); // 设置循环播放

    _player = FijkPlayer();
    _player.setDataSource(
        'http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4'
        // "http://vjs.zencdn.net/v/oceans.mp4"
        ); // 替换为你的视频URL
    _player.addListener(_playerValueChanged);
  }

  @override
  void dispose() {
    // _controller.dispose(); // 释放资源

    _player.release();
    super.dispose();
  }

  void _playerValueChanged() {
    // 视频播放状态变化的回调方法
    setState(() {});
  }

  Widget videoPlay() {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: 250,
      alignment: Alignment.center,
      child: FijkView(
        player: _player,
        fit: FijkFit.cover,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:
          // FutureBuilder(
          //     future: _initializeVideoPlayerFuture,
          //     builder: (context, snapshot) {
          //       if (snapshot.connectionState == ConnectionState.done) {
          //         return AspectRatio(
          //           aspectRatio: _controller.value.aspectRatio,
          //           child: Stack(
          //             alignment: Alignment.bottomCenter,
          //             children: [
          //               VideoPlayer(_controller),
          //               VideoProgressIndicator(
          //                 _controller,
          //                 allowScrubbing: true, // 允许快进
          //               ),
          //             ],
          //           ),
          //         );
          //       } else {
          //         return const Center(child: CircularProgressIndicator());
          //       }
          //     }),
          videoPlay(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // setState(() {
          //   if (_controller.value.isPlaying) {
          //     _controller.pause();
          //   } else {
          //     _controller.play();
          //   }
          // });
          if (_player.state == FijkState.started) {
            _player.pause();
          } else {
            _player.start();
          }
        },
        child: Icon(
          // _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
          _player.state == FijkState.started ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }
}
